import os
os.getcwd()
#返回当前工做目录
import numpy as np
import matplotlib.pyplot as plt
# % pylab
#初始化城市坐标，总共52个城市
coordinates = np.array([[565.0, 575.0], [25.0, 185.0], [345.0, 750.0], [945.0, 685.0], [845.0, 655.0],
                        [880.0, 660.0], [25.0, 230.0], [525.0, 1000.0], [580.0, 1175.0], [650.0, 1130.0],
                        [1605.0, 620.0], [1220.0, 580.0], [1465.0, 200.0], [1530.0, 5.0], [845.0, 680.0],
                        [725.0, 370.0], [145.0, 665.0], [415.0, 635.0], [510.0, 875.0], [560.0, 365.0],
                        [300.0, 465.0], [520.0, 585.0], [480.0, 415.0], [835.0, 625.0], [975.0, 580.0],
                        [1215.0, 245.0], [1320.0, 315.0], [1250.0, 400.0], [660.0, 180.0], [410.0, 250.0],
                        [420.0, 555.0], [575.0, 665.0], [1150.0, 1160.0], [700.0, 580.0], [685.0, 595.0],
                        [685.0, 610.0], [770.0, 610.0], [795.0, 645.0], [720.0, 635.0], [760.0, 650.0],
                        [475.0, 960.0], [95.0, 260.0], [875.0, 920.0], [700.0, 500.0], [555.0, 815.0],
                        [830.0, 485.0], [1170.0, 65.0], [830.0, 610.0], [605.0, 625.0], [595.0, 360.0],
                        [1340.0, 725.0], [1740.0, 245.0]])
def getdistmat(coordinates):
    num=coordinates.shape[0]
    distmat=np.zeros((num,num))
    distmat1=np.zeros((num,num))
    for i in range(num):
        for j in range(i, num):
            # 1测试np.linalg.norm
            #distmat[i][j] = distmat[j][i] =np.sqrt(pow(coordinates[i][0]-coordinates[j][0],2)+pow(coordinates[i][1]-coordinates[j][1],2))
            distmat1[i][j]=distmat1[j][i]=np.linalg.norm(coordinates[i]-coordinates[j],ord=2)
    return distmat,distmat1
# 1测试np.linalg.norm
# result,result1=getdistmat(coordinates=coordinates)
# print(result==result1)
distmat = getdistmat(coordinates)

numant = 60  # 蚂蚁个数
numcity = coordinates.shape[0]
# shape[0]=52 城市个数,也就是任务个数
alpha = 1  # 信息素重要程度因子
beta = 5   # 启发函数重要程度因子
rho = 0.1  # 信息素的挥发速度
Q = 1      # 完成率

iter = 0       #迭代初始
itermax = 150  #迭代总数

#etatable = 1.0 / (distmat + np.diag([1e10] * numcity))
etatable = 1.0 / (distmat + np.diag([1e10] * numcity))
#diag(),将一维数组转化为方阵 启发函数矩阵，表示蚂蚁从城市i转移到城市j的指望程度
pheromonetable = np.ones((numcity, numcity))
# 信息素矩阵 52*52
pathtable = np.zeros((numant, numcity)).astype(int)
# 路径记录表，转化成整型 40*52
#2.1测试np.diag，作用为将数组作为对角线创建数组
#print(np.diag([1e10] * numcity))
##2.2eye()函数：返回一个对角线上为1，其它地方为零的单位数组。
#print(np.diag([1e10] * numcity)==np.eye(numcity)*(1e10))

lengthaver = np.zeros(itermax)  # 迭代50次，存放每次迭代后，路径的平均长度  50*1
lengthbest = np.zeros(itermax)  # 迭代50次，存放每次迭代后，最佳路径长度  50*1
pathbest = np.zeros((itermax, numcity))  # 迭代50次，存放每次迭代后，最佳路径城市的坐标 50*52

while iter < itermax:
    #迭代总数
